package com.hibegin.ftp;

import javax.security.auth.login.LoginException;

public class FtpResponseImpl implements IFtpResponse {

    private FtpSession ftpSession;

    public FtpResponseImpl(FtpSession ftpSession) {
        this.ftpSession = ftpSession;
    }

    @Override
    public void connectSucc(String str) {
        ftpSession.sendMsg(new FtpRequestImpl(ftpSession).inputUserName());
    }

    @Override
    public void inputUserName(String str) {
        ftpSession.sendMsg(new FtpRequestImpl(ftpSession).inputUserName());
    }

    @Override
    public void inputPassword(String str) {
        ftpSession.sendMsg(new FtpRequestImpl(ftpSession).inputPassword());
    }

    @Override
    public void loginSucc(String str) {
        System.out.println("login succ");
        ftpSession.sendMsg(new FtpRequestImpl(ftpSession).changeToSYST());
        if (ftpSession.getAttr().get("callBack") != null) {
            ((FtpLoginSuccessCallBack) (ftpSession.getAttr().get("callBack"))).callBack(ftpSession);
        }

    }

    @Override
    public void cmdSucc(String str) {

    }

    @Override
    public void initCmdSucc(String str) {
        new FtpRequestImpl(ftpSession).sendCmd();
    }

    @Override
    public void modelChanageSucc(String str) {
        if (ftpSession.getAttr().get("callBack") != null) {
            ((FtpLoginSuccessCallBack) (ftpSession.getAttr().get("callBack"))).callBackSuccess(ftpSession);
        }
    }

    @Override
    public void passiveModeSucc(String str) {
        String addr = str.substring(str.indexOf("(") + 1, str.lastIndexOf(")"));
        ftpSession.getAttr().put("port", Integer.parseInt(addr.split(",")[4]) * 256
                + Integer.parseInt(addr.split(",")[5]));
    }

    @Override
    public void tipsMsg(String str) {
        new FtpRequestImpl(ftpSession).sendCmd();
    }

    @Override
    public void loginFailed(String str) throws LoginException {
        throw new LoginException("userName-> " + ftpSession.getFtpUser().getUserName() + " login failed");
    }
}
